//---------------------------------------------------------------------
// File: ResourceStringLoader.cs
// 
// Summary: Common methods used to retrieve messages and exception 
//          descriptions from resource files.
//
//
//---------------------------------------------------------------------
// This file is part of the Microsoft ESB Guidance for BizTalk
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 R2 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Globalization;
using System.Threading;
using System.Resources;
using System.Reflection;

namespace Microsoft.Practices.ESB.Exception.Management
{
    public static class ResourceStringLoader
    {       
       /// <summary>
       /// looks up message/descriptions based on string code, in resource file
       /// </summary>
       /// <param name="messageCode">constant representing lookup value</param>
       /// <param name="baseName">fully qualified name of resource file class</param>
       /// <param name="assembly">Executing assembly which the baseName class is in</param>
       /// <param name="arguments">optional arguments to format with the message 
       /// returned from the resource file</param>
       /// <returns>String from Resouce file that corresponds to messageCode</returns>
        public static string GetMessage(string messageCode, 
            string baseName,Assembly assembly, 
            params Object[] arguments)
        {
            if (null == assembly)
                throw new ArgumentNullException("assembly");
            if (string.IsNullOrEmpty(messageCode))
                throw new ArgumentNullException("messageCode");
            if (string.IsNullOrEmpty(baseName))
                throw new ArgumentNullException("baseName");
            try
            {
                // Get the culture of the currently executing thread.
                // The value of ci will determine the culture of
                // the resources that the resource manager retrieves.
                ResourceManager resourceManager = new ResourceManager(baseName, assembly);
                CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                // Retrieve the value of the string resource and return it 
                string message = resourceManager.GetString(messageCode, ci);

                return string.Format(CultureInfo.CurrentCulture, message, arguments);
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// looks up messag/descriptions based on string code, in resource file
        /// </summary>
        /// <param name="messageCode">constant representing lookup value</param>
        /// <param name="baseName">fully qualified name of resource file class</param>
        /// <param name="assembly">Executing assembly which the baseName class is in</param>
        /// <returns>String from Resouce file that corresponds to messageCode</returns>
        public static string GetMessage(string messageCode,
            string baseName, Assembly assembly)
        {
            if (null == assembly)
                throw new ArgumentNullException("assembly");
            if (string.IsNullOrEmpty(messageCode))
                throw new ArgumentNullException("messageCode");
            if (string.IsNullOrEmpty(baseName))
                throw new ArgumentNullException("baseName");

            try
            {
                // Get the culture of the currently executing thread.
                // The value of ci will determine the culture of
                // the resources that the resource manager retrieves.
                ResourceManager resourceManager = new ResourceManager(baseName, assembly);
                CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                // Retrieve the value of the string resource and return it 
                return resourceManager.GetString(messageCode, ci);
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// looks up exceptions descriptions based on error codes. prepends "e"
        /// for lookup in resource file
        /// </summary>
        /// <param name="errorCode">code represented by constant</param>
        /// <param name="baseName">fully qualified name of resource file class</param>
        /// <param name="assembly">Executing assembly which the baseName class is in</param>
        /// <returns>string representing error code</returns>
        public static string GetErrorDescription(int errorCode, 
            string baseName, Assembly assembly)
        {
            if (null == assembly)
                throw new ArgumentNullException("assembly");
            if (string.IsNullOrEmpty(baseName))
                throw new ArgumentNullException("baseName");

            try
            {
                // Get the culture of the currently executing thread.
                // The value of ci will determine the culture of
                // the resources that the resource manager retrieves.
                ResourceManager resourceManager = new ResourceManager(baseName, assembly);
                CultureInfo ci = Thread.CurrentThread.CurrentCulture;
                // Retrieve the value of the string resource and return it 
                return resourceManager.GetString("e" + System.Convert.ToString(errorCode, NumberFormatInfo.CurrentInfo), ci);
            }
            catch (System.Exception)
            {
                throw;
            }
        }
    }
}
